﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _2QWebProxy.App_Code;
using System.Net;

namespace _2QWebProxy.GUI
{
    public partial class ucProxyList : UserControl
    {
        #region Utils Methods

        private void LoadListProxy()
        {
            cbbProxy.Items.Clear();
            List<Proxy> proxy = Utils.GetProxyList();
            cbbProxy.Items.Add("Add new proxy");
            if (proxy.Count > 0)
            {
                foreach (Proxy oProxy in proxy)
                {
                    cbbProxy.Items.Add(oProxy);
                }
                cbbProxy.DisplayMember = "IpAddress";
            }
            cbbProxy.SelectedIndex = 0;
        }

        #endregion

        public ucProxyList()
        {
            InitializeComponent();
            LoadListProxy();
        }

        private void cbbProxy_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelNewProxy.Enabled = cbbProxy.SelectedIndex == 0;
            btnDelete.Enabled = !(cbbProxy.SelectedIndex == 0);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (cbbProxy.SelectedIndex == 0)
            {
                IPAddress ip;
                int port;
                if (IPAddress.TryParse(txtIPAddress.Text, out ip) && Int32.TryParse(txtPort.Text, out port))
                {
                    Proxy proxy = new Proxy()
                    {
                        IpAddress = txtIPAddress.Text,
                        Port = txtPort.Text
                    };
                    Utils.SaveProxyList(proxy);
                    LoadListProxy();
                    //cbbProxy.SelectedItem = proxy;
                    for (int i = 1; i < cbbProxy.Items.Count; i++)
                    {
                        if (((Proxy)cbbProxy.Items[i]).IpAddress.Equals(proxy.IpAddress) && ((Proxy)cbbProxy.Items[i]).Port.Equals(proxy.Port))
                        {
                            cbbProxy.SelectedIndex = i;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Invalid IpAddress or port");
                    return;
                }
            }

            //chainProxyToolStripMenuItem.Checked = !chainProxyToolStripMenuItem.Checked;
            Properties.Settings.Default.isChainProxy = true;
            Properties.Settings.Default.chainProxyIP = ((Proxy)cbbProxy.SelectedItem).IpAddress;
            Properties.Settings.Default.chainProxyPort = Int32.Parse(((Proxy)cbbProxy.SelectedItem).Port);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Utils.DeleteProxy((Proxy)cbbProxy.SelectedItem);
            LoadListProxy();
        }

        private void btnDisSelect_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.isChainProxy = false;
        }

    }
}
